#include "server.h"
#include "ui_server.h"
#include<QTime>
Server::Server(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Server)
{
    ui->setupUi(this);
    init();
}

Server::~Server()
{
    delete ui;
}

void Server::init()
{
    ts=new QTcpServer(this);
    connect(ts,&QTcpServer::newConnection,this,[=](){
        qDebug()<<"new socket comes";
        QTcpSocket* newtso=ts->nextPendingConnection();
        connect(newtso,&QTcpSocket::readyRead,this,[=](){
            QString info=newtso->readAll();
            QString mes;
            qDebug()<<"server26:"<<info;
            switch(ConfirmInfoType(info,&mes)){
            case INITWIDGET:
                InitWidget(mes,newtso);
                break;
            case ADDPEOPLE:
                AddNewPeople(mes);
                break;
            case SENDMES:
                SendMes(mes);
                break;
            default:
                qDebug()<<"server 31line:数据格式错误";
                break;
            }
        });
    });
    ts->listen(QHostAddress::Any,8081);
}

void Server::AddNewPeople(const QString &info)
{
    qDebug()<<"AddNewPeople";
    QString Sender,Receiver;
    GainAddPeopleMes(info,&Sender,&Receiver);
    qDebug()<<"server51"<<Sender<<" "<<Receiver;
    bool existpeople=false;
    if(widgets.contains(Receiver))
        existpeople=true;
    if(existpeople)
    {
        QString mestosender;
        SetAddPeopleMesForm(&mestosender,Receiver,widgets_image[Receiver]);

        QString mestoreceiver;
        SetAddPeopleMesForm(&mestoreceiver,Sender,widgets_image[Sender]);
        widgets[Sender]->write(mestosender.toUtf8());
        widgets[Receiver]->write(mestoreceiver.toUtf8());
    }
    else
    {
        QString mes;
        qDebug()<<"server67"<<"people no exist";
        SetAddPeopleMesForm(&mes,Sender);
        qDebug()<<"server69"<<mes;
        widgets[Sender]->write(mes.toUtf8());
    }
}

void Server::SendMes(const QString &info)
{
    QString sender,receiver,mes_time,contend_type;
    QVariant contend;
    GainSendMes(info,&sender,&receiver,& mes_time,& contend_type,&contend);
    qDebug()<<"server80"<<sender<<" "<<receiver<<" "<<mes_time<<" "<<contend_type<<" "<<contend;
    QString mestoreceiver;
    SetSendMesForm(&mestoreceiver,sender,mes_time,widgets_image[sender],contend_type,contend);
    qDebug()<<"server83"<<mestoreceiver;
    widgets[receiver]->write(mestoreceiver.toUtf8());

}

void Server::InitWidget(const QString &str,QTcpSocket* tso)
{
    int first=str.indexOf(":");
    int end=str.indexOf(";");
    QString name=str.sliced(first+1,end-first-1);
    if(widgets.contains(name))
        return;
    widgets[name]=tso;
    first=str.indexOf(":",first+1);
    end=str.indexOf(";",end+1);
    QString image=str.sliced(first+1,end-first-1);
    widgets_image[name]=image;
    qDebug()<<"server97"<<":客户端"<<name<<"成功创建";
}

void Server::GainAddPeopleMes(QString str, QString *sender, QString *receiver)
{
    int first=str.indexOf(":");
    int end=str.indexOf(";");
    *sender=str.sliced(first+1,end-first-1);
    first=str.indexOf(":",first+1);
    end=str.indexOf(";",end+1);
    *receiver=str.sliced(first+1,end-first-1);
}

void Server::GainSendMes(QString str, QString *sender, QString *receiver, QString *mes_time, QString *contend_type, QVariant *contend)
{
    int first=str.indexOf(":");
    int end=str.indexOf(";");
    *sender=str.sliced(first+1,end-first-1);
    first=str.indexOf(":",first+1);
    end=str.indexOf(";",end+1);
    *receiver=str.sliced(first+1,end-first-1);
    first=str.indexOf(":",first+1);
    end=str.indexOf(";",end+1);
    *mes_time=str.sliced(first+1,end-first-1);;
    first=str.indexOf(":",end+1);
    end=str.indexOf(";",end+1);
    *contend_type=str.sliced(first+1,end-first-1);
    first=str.indexOf(":",first+1);
    end=str.indexOf(";",end+1);
    *contend=str.sliced(first+1,end-first-1);
}


void Server::SetSendMesForm(QString *mes,const QString& sender_name , const QString &time,const QString &sender_image, const QString &contend_type, const QVariant &contend)
{
    //客户端协议
    //申请添加好友时:infotype:addpeople;exist:no;/exist:yes;image:path
    //接收服务端发来的信息时:infotype:sendmes;sender:name;sender-image:...;contend-type:...;contend:...;
    mes->append(QString("infotype:sendmes;"));
    mes->append(QString("sender:%1;").arg(sender_name));
    mes->append(QString("date:%1;").arg(time));
    mes->append(QString("sender-image:%1;").arg(sender_image));
    mes->append(QString("contend-type:%1;").arg(contend_type));
    mes->append(QString("contend:%1;").arg(contend.toString()));
}

void Server::SetAddPeopleMesForm(QString *mes, QString name,QString image)
{
    QString exist;
    if(image=="")
        exist="no";
    else
        exist="yes";
    *mes=QString("infotype:addpeople;exist:%1;name:%2;").arg(exist).arg(name);
    if(exist=="yes")
    {
        mes->append(QString("image:%1;").arg(image));
    }

}

Server::InfoType Server::ConfirmInfoType(const QString &str, QString *mes)
{
    int first=str.indexOf(":");
    int end=str.indexOf(";");
    QString infotype=str.sliced(first+1,end-first-1);
    *mes=str.sliced(end+1);
    qDebug()<<"server158:"<<*mes;
    if(infotype=="initwidget")
        return INITWIDGET;
    else if(infotype=="addpeople")
        return ADDPEOPLE;
    else if(infotype=="sendmes")
        return SENDMES;
}
